package com.nit.store.service.Impl;

import com.nit.store.service.UploadService;
import com.nit.store.service.UserService;
import com.nit.store.util.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author J1ay
 * @Date 2022/7/21 17:55
 * @Description
 */
@Slf4j
@Service
public class UploadServiceImpl implements UploadService {

    @Value("${upload.preUrl}")
    private String preUrl;

    @Value("${upload.askUrl}")
    private String askUrl;

    @Autowired
    private UserService userService;

    @Autowired
    private UploadUtils toolUtils;

    @Override
    public Map updateAvatar(MultipartFile file,  Long uid) {

        List<String> contentTypes = new ArrayList<>();
        contentTypes.add("image/png");
        contentTypes.add("image/jpeg");
        contentTypes.add("image/bmp");
        long maxSize = 10000*1024;//100K
        String fileName = toolUtils.upload(file,maxSize,contentTypes);
        String avatar = askUrl+fileName;
        System.out.println("avatar路径："+avatar);
        userService.updateAvatar(uid,fileName);
        Map map = new HashMap();
        // 图片上传后的文件名
        map.put("fileName", fileName);
        // 请求前缀
        map.put("askUrl", askUrl);
        return map;
    }
}
